﻿using System;
using System.Globalization;
using AccessControl.Business;
using AccessControl.Common.Enumerations;
using AccessControl.Utility.EnumHelper;
using DevExpress.XtraEditors;

namespace AccessControl.Management.Form
{
    public partial class ViewAllPerson : XtraForm
    {
        #region Field

        private static ViewAllPerson _viewAllPerson;

        private readonly PersianCalendar pc = new PersianCalendar();

        #endregion

        #region Singletone Member

        private ViewAllPerson()
        {
            InitializeComponent();
        }

        public static ViewAllPerson GetInstance()
        {
            if ((_viewAllPerson == null) || (_viewAllPerson.IsDisposed))
                _viewAllPerson = new ViewAllPerson();

            return _viewAllPerson;
        }

        #endregion

        #region Event Handler

        private void barButtonIteAddPerosn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddPerson.GetInstance().ShowDialog();
            RefresGrid();
        }

        private void barButtonItemEditPerson_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (CurrentPkPerson != String.Empty)
                EditPerson.GetInstance(CurrentPkPerson).ShowDialog();
            RefresGrid();
        }

        private void MainView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if ((e.Column == colPersonInfoDate) && (e.Value != null) && (e.Value != DBNull.Value))
            {
                var date = (DateTime)e.Value;
                e.DisplayText = String.Format("{0}/{1}/{2}", pc.GetYear(date), pc.GetMonth(date),
                                              pc.GetDayOfMonth(date));
            }
            if ((e.Column == colPersonExpireDate) && (e.Value != null) && (e.Value != DBNull.Value))
            {
                var date = (DateTime)e.Value;
                e.DisplayText = String.Format("{0}/{1}/{2}", pc.GetYear(date), pc.GetMonth(date),
                                              pc.GetDayOfMonth(date));
            }

            if (e.Column == colPersonIsMale)
            {
                if ((e.Value != null) && (e.RowHandle != -1))
                    try
                    {
                        if ((bool)e.Value)
                            e.DisplayText = "مذکر";
                        else
                            e.DisplayText = "مونث";
                    }
                    catch (Exception ex)
                    {
                    }
            }
            if((e.Column==colPersonTitle)&& (e.Value != null) && (e.Value != DBNull.Value))
            {
                e.DisplayText = ((PersonTitleConstants) ((byte) e.Value)).GetDescription();
            }
        }

        private void ViewAllPerson_Load(object sender, EventArgs e)
        {
            RefresGrid();
        }

        #endregion

        #region Private Method

        private void RefresGrid()
        {
            var tbl = PersonBusiness.SelectAllWithoutPhoto();
            gridControl.DataSource = tbl;
        }

        #endregion
        
        #region Property
        
        private String CurrentPkPerson
        {
            get
            {
                return gridView.SelectedRowsCount > 0
                        ? gridView.GetDataRow(gridView.GetSelectedRows()[0])["PkPerson"].ToString()
                        : String.Empty;
            }

        }

        #endregion
    }
}